/*
 * Created by JFormDesigner on Mon Dec 13 11:12:25 CST 2021
 */

package com.cn.项目.飞机预约管理系统.Maininterface;

import com.cn.项目.utils.JdbcUtils;

import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.event.*;

/**
 * @author Brainrain
 */
// 设置公告
public class SetAnnouncement extends JPanel {
    public SetAnnouncement() {
        initComponents();
    }

    //设置
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String text = textPane1.getText();
        Connection conn = null;
        PreparedStatement st = null;

        try {
           conn = JdbcUtils.getConnection();
           String sql = "insert into announcement(atext)values (?)";

            st = conn.prepareStatement(sql);
            st.setString(1, text);
            int i = st.executeUpdate();
            if (i>0){
              new Successprompt();
           }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }finally {
            JdbcUtils.release(conn, st, null);
        }
    }

    //重置
    private void button2AncestorAdded(AncestorEvent e) {
        // TODO add your code here
        textPane1.setText("");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        textPane1 = new JTextPane();
        label1 = new JLabel();
        button1 = new JButton();
        button2 = new JButton();

        //======== this ========
        setLayout(null);

        //======== scrollPane1 ========
        {

            //---- textPane1 ----
            textPane1.setText("                                         在公告最后写上时间");
            scrollPane1.setViewportView(textPane1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(5, 80, 395, 220);

        //---- label1 ----
        label1.setText("请您设置公告");
        label1.setForeground(new Color(0, 153, 153));
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 12f));
        add(label1);
        label1.setBounds(115, 25, 200, 40);

        //---- button1 ----
        button1.setText("设置");
        button1.setForeground(new Color(51, 255, 51));
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 6f));
        button1.addActionListener(e -> button1ActionPerformed(e));
        add(button1);
        button1.setBounds(80, 320, 90, 35);

        //---- button2 ----
        button2.setText("重置");
        button2.setFont(button2.getFont().deriveFont(button2.getFont().getSize() + 6f));
        button2.setForeground(Color.red);
        button2.addAncestorListener(new AncestorListener() {
            @Override
            public void ancestorAdded(AncestorEvent e) {
                button2AncestorAdded(e);
            }
            @Override
            public void ancestorMoved(AncestorEvent e) {}
            @Override
            public void ancestorRemoved(AncestorEvent e) {}
        });
        add(button2);
        button2.setBounds(new Rectangle(new Point(240, 320), button2.getPreferredSize()));

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JTextPane textPane1;
    private JLabel label1;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
